package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.csiec.clock.R;
import com.csiec.clock.activity.FaceActivity;
import com.csiec.clock.activity.MainActivity;
import com.csiec.clock.activity.UserInfoActivity;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.face.FaceInfo;
import com.csiec.clock.callback.LoginCallBack;
import com.csiec.clock.callback.UserInfoCallBack;
import com.csiec.clock.databinding.ActivityUserInfoBinding;
import com.csiec.clock.model.UserInfoModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class UserInfoViewModel extends ViewModel {

    private final String TAG = UserInfoViewModel.class.getSimpleName();

    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityUserInfoBinding binding;
    private String checkFace;

    private UserInfoModel userInfoModel;
    private MutableLiveData<String> studentNumber;
    private MutableLiveData<String> faceInfo;
    private MutableLiveData<String> position;
    private final Handler handler = new Handler();

    public UserInfoViewModel() {
    }

    public void init(Activity activity, ActivityUserInfoBinding binding, String checkFace) {
        this.activity = activity;
        this.binding = binding;
        this.checkFace = checkFace;
        userInfoModel = new UserInfoModel();
        if (studentNumber == null) {
            studentNumber = new MutableLiveData<>();
        }
        if (faceInfo == null) {
            faceInfo = new MutableLiveData<>();
            faceInfo.setValue(checkFace);
        }
        if (position == null) {
            position = new MutableLiveData<>();
        }
    }

    //
    @SuppressLint("ResourceAsColor")
    public void getFace() {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("启动界面中...")
                .create();
        tipDialog.show();
        Intent intent = new Intent(activity, FaceActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("activityInfo", "0");
        activity.startActivity(intent);
        binding.textView30.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
                activity.finish();
            }
        }, 2000);

    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView30.postDelayed(tipDialog::dismiss, 1000);
    }


    public void edit() {
        //修改信息
        if (studentNumber.getValue() != null && UserInfo.getUserInfo().getPosition() != null && UserInfo.getUserInfo().getBase64() != null) {
            if ("验证成功".equals(checkFace)) {
                UserInfo.getUserInfo().setStudentNumber(studentNumber.getValue());
                userInfoModel.edit(new UserInfoCallBack() {
                    @Override
                    public void onSuccess(String name) {
                        if (!name.equals("fail")) {
                            handler.post(() -> {
                                showTip(QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "修改信息成功");
                            });
                        } else {
                            handler.post(() -> {
                                showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "人脸验证失败");
                            });
                        }
                    }

                    @Override
                    public void onFailed() {
                        handler.post(() -> {
                            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络错误或其它错误");
                        });
                    }
                });
            } else {
                showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "人脸验证失败");
            }

        } else {
            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "请完善基本信息");
        }

    }

    public MutableLiveData<String> getStudentNumber() {
        return studentNumber;
    }

    public void setPosition(String address) {
        position.setValue(address);
    }

    public MutableLiveData<String> getPosition() {
        return position;
    }

    public MutableLiveData<String> getFaceInfo() {
        return faceInfo;
    }
}
